package com.victor.common.security.interceptor;

import cn.hutool.core.util.StrUtil;
import com.victor.common.core.constant.SecurityConstants;
import com.victor.common.core.context.SecurityContextHolder;
import com.victor.common.core.utils.JwtUtils;
import com.victor.common.core.utils.ServletUtils;
import com.victor.common.core.utils.StringUtils;
import com.victor.common.response.system.LoginUser;
import com.victor.common.security.auth.AuthUtil;

import com.victor.common.security.utils.SecurityUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.AsyncHandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义请求头拦截器，将Header数据封装到线程变量中方便获取
 * 注意：此拦截器会同时验证当前用户有效期自动刷新有效期
 *
 */
public class HeaderInterceptor implements AsyncHandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        if (!(handler instanceof HandlerMethod))
        {
            return true;
        }
        SecurityContextHolder.setCompanyInfo(ServletUtils.getHeader(request, SecurityConstants.COMPANY_INFO));
        SecurityContextHolder.setUserId(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USER_ID));
        SecurityContextHolder.setUserName(ServletUtils.getHeader(request, SecurityConstants.DETAILS_USERNAME));
        SecurityContextHolder.setUserKey(ServletUtils.getHeader(request, SecurityConstants.USER_KEY));
        SecurityContextHolder.setHospitalId(ServletUtils.getHeader(request, SecurityConstants.HOSPITAL_ID));
        SecurityContextHolder.setHospitalName(ServletUtils.getHeader(request, SecurityConstants.HOSPITAL_NAME));
        SecurityContextHolder.setAffiliatedId(ServletUtils.getHeader(request, SecurityConstants.AFFILIATED_ID));
        SecurityContextHolder.setAffiliatedName(ServletUtils.getHeader(request, SecurityConstants.AFFILIATED_NAME));
        SecurityContextHolder.setCategoryCode(ServletUtils.getHeader(request, SecurityConstants.CATEGORY_CODE));
        SecurityContextHolder.setThirdNo(ServletUtils.getHeader(request, SecurityConstants.THIRD_NO));

        String token = SecurityUtils.getToken();
        if(SecurityContextHolder.getUserId()==0 && StrUtil.isNotBlank(token)){
            SecurityContextHolder.setUserId(JwtUtils.getUserId(token));
            SecurityContextHolder.setUserName(JwtUtils.getUserName(token));
            SecurityContextHolder.setUserKey(JwtUtils.getUserKey(token));
            SecurityContextHolder.setHospitalId(JwtUtils.getUserHospitalId(token));
            SecurityContextHolder.setAffiliatedId(JwtUtils.getUserAffiliatedId(token));
            SecurityContextHolder.setHospitalName(JwtUtils.getUserHospitalName(token));
            SecurityContextHolder.setAffiliatedName(JwtUtils.getUserAffiliatedName(token));
            SecurityContextHolder.setCategoryCode(JwtUtils.getUserCategoryCode(token));
            SecurityContextHolder.setThirdNo(JwtUtils.getUserThirdNo(token));
        }

        if (StringUtils.isNotEmpty(token))
        {
            LoginUser loginUser = AuthUtil.getLoginUser(token);
            if (StringUtils.isNotNull(loginUser))
            {
                AuthUtil.verifyLoginUserExpire(loginUser);
                SecurityContextHolder.set(SecurityConstants.LOGIN_USER, loginUser);
            }
        }
        return true;
    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception
    {
        SecurityContextHolder.remove();
    }
}
